<template>
	<view class="container">

		<view class="nav-bar">
			<view class="back-icon" @tap="goBack">
				<uni-icons type="left" size="20" color="#fff"></uni-icons>
			</view>
			<view class="title">我的车辆</view>
			<view class="right-icons">
				<uni-icons type="more-filled" size="20" color="#fff"></uni-icons>
			</view>
		</view>

		<view class="content-box">
			<!-- 空状态 -->
			<view class="empty-state white-box" v-if="!carList.length">
				<image class="empty-img" src="/static/组 4.png" />
				<text class="empty-text">暂无车辆信息</text>
			</view>

			<!-- 车辆列表 -->
			<view class="car-list" v-else>
				<view class="car-item white-box" v-for="(item, index) in carList" :key="index">
					<view class="car-info">
						<image class="car-image" :src="item.image" mode="aspectFill" />
						<view class="car-detail">
							<text class="car-name">车型：{{ item.carModel }}</text>
							<text class="car-number">车牌：{{ item.carNumber }}</text>
						</view>
					</view>
					<view class="car-actions">
						<view class="default-setting" @tap="setDefaultCar(index)">
							<radio :checked="item.isDefault" color="#4080FF" style="transform:scale(0.8)" />
							<text>设为默认车辆</text>
						</view>
						<text class="delete-btn" @tap="deleteCar(index)">删除</text>
					</view>
				</view>
			</view>

			<!-- 底部按钮 -->
			<view class="bottom-btn">
				<button class="add-btn" @tap="goToAdd">添加爱车</button>
			</view>

		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				carList: [{
						carModel: "奔驰-GR",
						carNumber: "豫P CC531",
						image: "https://img0.baidu.com/it/u=3454687072,3865494486&fm=253&fmt=auto&app=120&f=JPEG?w=889&h=500",
						isDefault: true
					}, {
						carModel: "梅赛德斯-AMG A ",
						carNumber: "京E kl787",
						image: "https://img0.baidu.com/it/u=2839061023,559960605&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=500",
						isDefault: false
					},
					{
						carModel: "奔驰s206 ",
						carNumber: "鲁A 88888",
						image: "https://i1.hdslb.com/bfs/archive/ba83382519cf213f90f195f3306cbcad4097db44.jpg",
						isDefault: false
					}
				]
			}
		},
		methods: {
			setDefaultCar(index) {
				// 先将所有车辆设为非默认
				this.carList.forEach(car => {
					car.isDefault = false
				})
				// 设置选中的车辆为默认
				this.carList[index].isDefault = true

			},
			goBack() {
				uni.navigateTo({
					url: "/pages/lz-MyWeiXiu/myrepair/myrepair"
				})
			},
			goToAdd() {
				uni.navigateTo({
					url: '/pages/lz-MyCheLiang/addmycar/addmycar'
				})
			},
			deleteCar(index) {
				uni.showModal({
					title: '提示',
					content: '确定要删除该车辆吗？',
					success: (res) => {
						if (res.confirm) {
							this.carList.splice(index, 1)
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.nav-bar {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		z-index: 100;
		padding: var(--status-bar-height) 16px 12px;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.title {
			color: #fff;
			font-size: 17px;
			font-weight: 500;
		}
	}

	.my-cars {
		min-height: 100vh;
		background: #fff;
		padding: 30rpx;
	}

	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: 200rpx;
		height: 800rpx;

		.empty-img {
			margin-right: 110rpx;
		}

		.empty-text {
			margin-top: 20rpx;
			color: #999;
			font-size: 28rpx;
		}
	}

	.car-list {
		.car-item {
			padding: 20rpx;
			border-bottom: 1px solid #eee;

			.car-info {
				display: flex;
				align-items: center;

				.car-image {
					width: 120rpx;
					height: 120rpx;
					border-radius: 8rpx;
				}

				.car-detail {
					margin-left: 20rpx;

					.car-name,
					.car-number {
						display: block;
						font-size: 28rpx;
						color: #333;
						margin-bottom: 10rpx;
					}
				}
			}

			.car-actions {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-top: 20rpx;

				.delete-btn {
					color: #ff4d4f;
					font-size: 28rpx;
				}
			}
		}
	}

	.bottom-btn {
		position: fixed;
		left: 30rpx;
		right: 30rpx;
		bottom: 40rpx;

		.add-btn {
			background: #1890FF;
			color: #fff;
			border-radius: 8rpx;
			font-size: 32rpx;
		}
	}
</style>